package com.ruoyi.driver.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.driver.domain.Car;
import com.ruoyi.driver.service.IDriverService;
import com.ruoyi.driver.utils.ShuMaiUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 司机Controller
 * 
 * @author lipengze
 * @date 2024-10-17
 */
@Slf4j
@RestController
@RequestMapping("/driver")
public class DriverController extends BaseController
{

    @Resource
    private IDriverService driverService;
    @Autowired
    private ShuMaiUtils shuMaiUtils;

    /**
     * 上传身份证图片
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upLoadFile")
    public AjaxResult upLoadFile(MultipartFile file) throws IOException {
        //调用service处理身份证正反面逻辑
        AjaxResult ajaxResult = driverService.idCardOcr(file);
        log.info("服务器识别结果--->>>>{}" ,ajaxResult);
        return ajaxResult;
    }

    /**
     * 上传驾驶证
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upLoadDriving")
    public AjaxResult upLoadDriving(MultipartFile file,@RequestParam String userInfo) throws IOException {
        //调用service处理驾驶证相关逻辑
        AjaxResult ajaxResult = driverService.drivingOcr(file,shuMaiUtils.handleApiResponse(userInfo));
        log.info("服务器识别结果--->>>>{}",ajaxResult);
        return ajaxResult;
    }

    /**
     * 上传行驶证
     * @param file  行驶证照片
     * @return
     * @throws IOException
     */
    @PostMapping("/upLoadVehicle")
    public Map<String, Object> upLoadVehicle(MultipartFile file) throws IOException {
        AjaxResult ajaxResult = driverService.vehicleOcr(file);
        log.info("服务器识别结果--->>>>{}",ajaxResult);
        return ajaxResult;
    }

    /**
     * 视频活体检测 &&特征检测 &&同人检测
     * @param file
     * @return
     */
    @PostMapping("/face")
    public Map<String, Object> face(MultipartFile file,Map<String,Object> map) throws IOException {
        AjaxResult ajaxResult = driverService.recognizeFace(file,map);
        return ajaxResult;
    }

    /**
     * 查询司机列表
     */
    @RequiresPermissions("driver:driver:list")
    @GetMapping("/list")
    public TableDataInfo list(Car car)
    {
        startPage();
        List<Car> list = driverService.selectCarList(car);
        return getDataTable(list);
    }
    /**
     * 导出司机列表
     */
    @RequiresPermissions("driver:driver:export")
    @Log(title = "司机", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Car car)
    {
        List<Car> list = driverService.selectCarList(car);
        ExcelUtil<Car> util = new ExcelUtil<Car>(Car.class);
        util.exportExcel(response, list, "司机数据");
    }

    /**
     * 获取司机详细信息
     */
    @RequiresPermissions("driver:driver:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(driverService.selectCarById(id));
    }

    /**
     * 新增司机
     */
    @RequiresPermissions("driver:driver:add")
    @Log(title = "司机", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Car car)
    {
        return toAjax(driverService.insertCar(car));
    }

    /**
     * 修改司机
     */
    @RequiresPermissions("driver:driver:edit")
    @Log(title = "司机", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Car car)
    {
        return toAjax(driverService.updateCar(car));
    }

    /**
     * 删除司机
     */
    @RequiresPermissions("driver:driver:remove")
    @Log(title = "司机", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(driverService.deleteCarByIds(ids));
    }
}
